<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="200"
    >
      <template #beforeworkShop>
        <el-table-column
          label="票据号"
          align="center"
          key="billNumber"
          prop="billNumber"
          width="200"
        />
      </template>
      <!-- <template #applyCompanyId="{ row }">{{ row.applyCompany }}</template> -->

      <template #jobPersonnel="{ row }"> {{ row.jobPersonnelName }}</template>
      <template #monitorLocal="{ row }"> {{ row.monitorLocalName }}</template>
      <template #gasLocal="{ row }"> {{ row.gasLocalName }}</template>
      <template #jobType="{ row }"> {{ row.jobTypeName }}</template>
      <template #default>
        <el-table-column
          label="审批状态"
          width="200"
          prop="status"
          align="center"
        >
          <template #default="{ row }">
            <span v-if="row.status == 1"> 已通过 </span>
            <span v-else-if="row.status == 0"> 已驳回 </span>
            <span v-else> 待审核 </span>
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" @click="handleEdit(row)">查看</el-button>
        <el-button
          type="text"
          :disabled="row.status == 0 || row.status == 1"
          @click="handleAppro(row, 1)"
          >通过</el-button
        >
        <el-button type="text" style="color: red" @click="handleAppro(row, 0)"
          >驳回</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      title="作业计划"
      :slots="[
        'custody',
        'coordinate',
        'jobPersonnel',
        'jobPersonCharge',
        'approvedBy',
        'examinationPeople',
        // 'monitorLocal',
        // 'gasLocal',
      ]"
      :showButton="false"
    >
      <template #monitorLocal>
        <el-form-item label="涉及监控点位" label-position="top">
          <el-table ref="dragTable" :data="form.monitorLocal">
            <el-table-column
              label="监控点位名称"
              prop="monitorName"
              :show-overflow-tooltip="true"
            />
          </el-table>
        </el-form-item>
      </template>
      <template #gasLocal>
        <el-form-item label="涉及气体位号" label-position="top">
          <el-table ref="dragTable" :data="form.gasLocal">
            <el-table-column
              label="气体位号"
              prop="bitNumber"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="安装位置"
              prop="installAddress"
              :show-overflow-tooltip="true"
            />
          </el-table>
        </el-form-item>
      </template>
      <template #jobPersonnel>
        <el-form-item label="作业人员" label-position="right">
          <el-table ref="dragTable" :data="form.jobPersonnel1">
            <el-table-column
              label="姓名"
              align="center"
              key="name"
              prop="name"
            />
            <el-table-column
              label="证书号"
              align="center"
              key="zsh"
              prop="zsh"
            />

            <el-table-column label="人员类型" align="center">
              <template #default="{ row }">
                {{ row.type == "in" ? "内部人员" : "外部人员" }}
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </template>
      <template v-for="(item, key) of slots" #[key]>
        <el-form-item :label="item" :prop="key" label-position="right">
          <el-input v-model="form[key]" readonly @click="onInnerOpen(key)">
          </el-input>
        </el-form-item>
      </template>

      <TableSelect
        ref="userTableSelect"
        :queryApi="listUser"
        :options="[]"
        :rowKey="'userId'"
        rowLabel="nickName"
        @onConfirm="onUserConfirm"
        rowLabelText="用户昵称"
        :maxNum="1"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.userName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="dept.deptName"
          />
          <el-table-column
            label="用户名称"
            align="center"
            key="userName"
            prop="userName"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            key="nickName"
            prop="nickName"
          />
        </template>
      </TableSelect>
    </DialogVform>
    <MapDialog ref="map" mapId="zypsh" :useButton="false"></MapDialog>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/zygl/zysb.js";
import formJson from "./components/options.json";
import { deptTreeSelect, listUser } from "@/api/system/user";
import TableSelect from "@/components/tableSelect/index";

import MapDialog from "@/components/mapDialog/index";
import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
const { proxy } = getCurrentInstance();
const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "riskLevel",
      "applyCompany",
      "degree",
      "jobPlanStartDate",
      "izManual",
      "billNumber",
      "local",
      "workShop",
      "declareType",
    ].includes(i.prop)
  ),
});
const slots = ref({
  // custody: "属地监护人",
  coordinate: "施工协调员",
  jobPersonCharge: "作业负责人",
  approvedBy: "现场批准人",
  examinationPeople: "措施审查人",
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();
const userTableSelect = ref();
const userType = ref("");

// 补充修改.新增事件

const handleAppro = (row, status) => {
  const text = status === 1 ? "通过" : "驳回";
  proxy.$modal
    .confirm("是否确认" + text)
    .then(function () {
      return APIS.approval({ status, billCard: row.billNumber });
    })
    .then(() => {
      proxy.$modal.msgSuccess("操作成功");
      getList();
    })
    .catch(() => {});
};
const plugEdit = (row) => {
  form.value.jobPersonnel1 = row.jobPersonnel || [];
  form.value.jobPersonnel = row.jobPersonnel;
  form.value.monitorLocal = row.monitorLocal;
  form.value.gasLocal = row.gasLocal;
  form.value.jobType = row.jobType;
  form.value.custody = row.custody;
  form.value.coordinate = row.coordinate;
  form.value.jobPersonnel = row.jobPersonnel;
  form.value.jobPersonCharge = row.jobPersonCharge;
  form.value.approvedBy = row.approvedBy;
  form.value.examinationPeople = row.examinationPeople;
  form.value.custodyId = row.custodyId;
  form.value.coordinateId = row.coordinateId;
  form.value.jobPersonnelId = row.jobPersonnelId;
  form.value.jobPersonChargeId = row.jobPersonChargeId;
  form.value.approvedById = row.approvedById;
  form.value.examinationPeopleId = row.examinationPeopleId;
  form.value.applyCompanyId = row.applyCompanyId;
};
const formatList = (list) => {
  return list.map((item) => {
    let jobPersonnel = [];
    let monitorLocal = [];
    let gasLocal = [];
    try {
      jobPersonnel = JSON.parse(item.jobPersonnel) || [];
    } catch (error) {
      jobPersonnel = [];
    }
    try {
      monitorLocal = JSON.parse(item.monitorLocal) || [];
    } catch (error) {
      monitorLocal = [];
    }
    try {
      gasLocal = JSON.parse(item.gasLocal) || [];
    } catch (error) {
      gasLocal = [];
    }
    return {
      ...item,
      jobPersonnel,
      monitorLocal,
      gasLocal,
      jobType: (item.jobType && item.jobType.split(",")) || [],
      jobPersonnelName: jobPersonnel.reduce((acc, item) => {
        return (acc += item.name);
      }, ""),
      monitorLocalName: monitorLocal.reduce((acc, item) => {
        return (acc += item.monitorName);
      }, ""),
      gasLocalName: gasLocal.reduce((acc, item) => {
        return (acc += item.bitNumber);
      }, ""),
      jobTypeName: item.jobType,
    };
  });
};
const map = ref();
const points = ref([]);
const openMap = (row) => {
  if (row && row.drawArea) {
    useButton.value = false;
    map.value.open(JSON.parse(row.drawArea));
    return;
  }
  if (row) {
    useButton.value = false;
    map.value.open([]);
  } else {
    useButton.value = true;
    map.value.open(points.value);
  }
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  getList,
  queryParams,
  dialogType,
  form,
  handleEdit,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  formatList,
});
</script>

<style lang="scss" scoped></style>
